package com.kingdee.pda.ui.check_order;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.example.basecommon.http.MyObserver;
import com.example.basecommon.http.RxHelper;
import com.example.basecommon.http.rxjava.BaseResponse;
import com.example.basecommon.http.rxjava.HttpUtils;
import com.example.basecommon.utils.ConstantUtil;
import com.kingdee.pda.Api;
import com.kingdee.pda.entity.CheckOrderDetailBean;
import com.kingdee.pda.entity.InvstatusBean;
import com.kingdee.pda.entity.OrderSubmitBean;
import com.kingdee.pda.entity.ProductOwnerBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class OrderDetailPresenter implements DetailContract.IDetailPresenter {

    Context context;
    DetailContract.IDetailView view;

    public OrderDetailPresenter(Context context, DetailContract.IDetailView view) {
        this.context = context;
        this.view = view;
    }

    @Override
    public void getOrderDetail(String bilNum, String bilStatus) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("billno", bilNum);
        map.put("billstatus", bilStatus);

        MyObserver<CheckOrderDetailBean> myObserver = new MyObserver<CheckOrderDetailBean>(context) {

            @Override
            public void onSuccess(BaseResponse<CheckOrderDetailBean> result) {
                view.getDetailSuccess(result.data);
            }

            @Override
            public void onFailure(BaseResponse result, String msg) {
                view.getDetailFail(msg);
            }
        };

        HttpUtils.request(Api.class)
                .getCheckOrderDetail(ConstantUtil.CHECK_ORDER_DETAIL, map)
                .compose(RxHelper.observableIO2Main(context))
                .subscribe(myObserver);
    }


    @Override
    public void getAllInvStatus() {
        MyObserver<List<InvstatusBean>> myObserver = new MyObserver<List<InvstatusBean>>(context) {
            @Override
            public void onSuccess(BaseResponse<List<InvstatusBean>> result) {
                view.getInvstatusSuccess(result.data);
            }

            @Override
            public void onFailure(BaseResponse<List<InvstatusBean>> result, String msg) {
                view.getInvstatusFail(msg);
            }
        };

        HttpUtils.request(Api.class).getAllInvstatus(ConstantUtil.GET_ALL_INVSTATUS).compose(RxHelper.observableIO2Main(context)).subscribe(myObserver);
    }

    @Override
    public void getProductOwner(String itemBarcode, String warehouseNum, String invstatusId, boolean invlibout) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("itembarcode", itemBarcode);
        if (!TextUtils.isEmpty(warehouseNum)) {
            map.put("locationnumber", warehouseNum);
        }
        if (!TextUtils.isEmpty(invstatusId)) {
            map.put("invstatusId", invstatusId);
        }
        map.put("invlibout", invlibout);
        MyObserver<List<ProductOwnerBean>> myObserver = new MyObserver<List<ProductOwnerBean>>(context) {
            @Override
            public void onSuccess(BaseResponse<List<ProductOwnerBean>> result) {
                view.getProductOwnerSuccess(result.data);
            }

            @Override
            public void onFailure(BaseResponse<List<ProductOwnerBean>> result, String msg) {
                view.getProductFail(msg);
            }
        };

        HttpUtils.request(Api.class).getProductOwner(ConstantUtil.GET_PRODUCT_OWNER, map).compose(RxHelper.observableIO2Main(context)).subscribe(myObserver);

    }

    @Override
    public void commitOrder(OrderSubmitBean submitBean) {
        Log.d("commit",submitBean.toString());
        MyObserver observer = new MyObserver(context) {
            @Override
            public void onSuccess(BaseResponse result) {
                view.commitOrderSuccess("提交成功");
            }
            @Override
            public void onFailure(BaseResponse result, String msg) {
                view.commitOrderFail(msg);
            }
        };
        HttpUtils.request(Api.class).commitCheckOrder(ConstantUtil.SUBMIT_CHECK_ORDER, submitBean).compose(RxHelper.observableIO2Main(context)).subscribe(observer);
    }

    private OrderSubmitBean mockCommit() {
        OrderSubmitBean bean = new OrderSubmitBean();
        bean.inventorynumber = "WCO-20220829-00000014";
        bean.invdetails = new ArrayList<>();
        OrderSubmitBean.Invdetail invdetail = new OrderSubmitBean.Invdetail();
        invdetail.ownertype = "bos_org";
        invdetail.ownerid = "100000";
        invdetail.skuid = "1437654516915775488";
        invdetail.itembarcode = "itembatch2";
        invdetail.invnumber = "1";
        invdetail.locationid = "1291993734979794944";
        invdetail.invstatusid = "691928582720825344";
        bean.invdetails.add(invdetail);
        Log.d("submit", "mock submit" + bean.toString());
        return bean;

    }
}
